package com.cx.mall.user.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.mall.common.enums.ResponseEnum;
import com.cx.mall.user.entity.UserInfo;
import com.cx.mall.user.mapper.UserMapper;
import com.cx.mall.user.service.IUserService;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserInfo> implements IUserService {
    @Override
    public UserInfo login(String username, String password) throws Exception {
        // 按用户名查询用户
        LambdaQueryChainWrapper<UserInfo> queryChainWrapper = new
                LambdaQueryChainWrapper<>(baseMapper);
        UserInfo userInfo = queryChainWrapper.eq(UserInfo::getUsername, username)
                .eq(UserInfo::getStatus,1)
                .one();
        // 判断密码是否正确
       /* if(userInfo != null){
            if(password.equals(userInfo.getPassword())){
                return userInfo;
            }else {
                throw new Exception("密码错误");
            }
        }else {
            throw new Exception("用户名不存在或已被禁用");
        }*/
        // 判断密码是否正确
        if(userInfo != null){
            if(BCrypt.checkpw(password,userInfo.getPassword())){
                return userInfo;
            }else {
                throw new Exception(ResponseEnum.USER_PASSWORD_ERROR.getMessage());
            }
        }else {
            throw new Exception(ResponseEnum.USER_ACCOUNT_FORBIDDEN.getMessage());
        }
    }
}